/*
 *	xmem.c
 *	  part of the xlibrary by SunDEV (http://sundev.890m.com)
 *
 *	Changelog :
 *	  ##-##-## : First public release
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>	// for mallinfo
#include <sys/unistd.h>	// for sbrk()
#include "xcommon.h"

#include "xmem.h"

#if defined(ARM9) || defined(ARM7)
extern u8 __end__[];        // end of static code and data
extern u8 __eheap_end[];    // farthest point to which the heap will grow

u8 *getHeapStart() {
	return __end__;
}

u8 *getHeapEnd() {
	return (u8 *)sbrk(0);
}

u8 *getHeapLimit() {
	return __eheap_end;
}
#endif	// defined(ARM9) || defined(ARM7)

size_t getMemUsed() {
	struct mallinfo mi = mallinfo();
	return mi.uordblks;
}

#if defined(ARM9) || defined(ARM7)
size_t getMemFree() {
	struct mallinfo mi = mallinfo();
	return mi.fordblks + (getHeapLimit() - getHeapEnd());
}
#endif	// defined(ARM9) || defined(ARM7)

